package com.yungu.swift.assets.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.yungu.swift.base.model.config.CommonConfig;
import com.yungu.swift.base.model.config.DriverConfig;
import com.yungu.swift.base.model.config.ServerConfig;
import com.yungu.swift.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author : cuixiuyin
 * @date : 2019/9/11
 */
@Slf4j
@Configuration
public class ApplicationConfig {

    @Value("${nacos.host:127.0.0.1:8848}")
    private String host;
    @Value("${nacos.namespace:}")
    private String namespace;
    private String GROUP = "DEFAULT_GROUP";

    public static CommonConfig COMMON_CONFIG;
    public static ServerConfig SERVER_CONFIG;
    public static DriverConfig DRIVER_CONFIG;

    @PostConstruct
    public void init() {
        Properties properties = new Properties();
        properties.put("serverAddr", host);
        properties.put("namespace", namespace);
        try {
            ConfigService configService = NacosFactory.createConfigService(properties);
            // 通用配置项监听
            configService.addListener("COMMON_CONFIG", GROUP, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {
                    log.info("receive COMMON_CONFIG value:{}", configInfo);
                    try {
                        COMMON_CONFIG = Optional.ofNullable(JsonUtils.readValue(configInfo, CommonConfig.class)).orElse(new CommonConfig());
                    } catch (IOException e) {
                        log.error("NacosListener init Listener JsonUtils readValue COMMON_CONFIG error.", e);
                    }
                }
            });
            // 服务端配置项监听
            configService.addListener("SERVER_CONFIG", GROUP, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {
                    log.info("receive SERVER_CONFIG value:{}", configInfo);
                    try {
                        SERVER_CONFIG = Optional.ofNullable(JsonUtils.readValue(configInfo, ServerConfig.class)).orElse(new ServerConfig());
                    } catch (IOException e) {
                        log.error("NacosListener init Listener JsonUtils readValue SERVER_CONFIG error.", e);
                    }
                }
            });
            // 司机端配置项监听
            configService.addListener("DRIVER_CONFIG", GROUP, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {
                    log.info("receive DRIVER_CONFIG value:{}", configInfo);
                    try {
                        DRIVER_CONFIG = Optional.ofNullable(JsonUtils.readValue(configInfo, DriverConfig.class)).orElse(new DriverConfig());
                    } catch (IOException e) {
                        log.error("NacosListener init Listener JsonUtils readValue DRIVER_CONFIG error.", e);
                    }
                }
            });
        } catch (NacosException e) {
            log.error("NacosListener init createConfigService error.", e);
        }
    }
}
